package com.wicket.okrframework.web.contorller;

import com.wicket.okrframework.common.ro.OkrResult;
import com.wicket.okrframework.common.util.CommonFunctionHelper;
import com.wicket.okrframework.integration.dto.spec.FwQueryTableList;
import com.wicket.okrframework.integration.dto.spec.FwTableClietn;
import com.wicket.okrframework.integration.dto.spec.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/design/")
public class DesignController {
    @Resource
    private FwTableClietn client;

    @Resource
    private FwQueryTableList tableClient;
    @PostMapping("tableinfo")
    public OkrResult<QueryTableInfoRespDto> putSpaceInfoToRedis(@RequestBody @Validated QueryTableInfoReqDto req) {
        ListTableRespDto listTableRespDto = tableClient.listTable("JSESSIONID=ceba142c-4d8d-494e-9d36-b9cbbc7742a7; rememberMe=pVUHSSx1v0U8dKN4EtPHZxWT7WdyeGFu6u0wI6asvnCi5HQjSUH7tvQlkC0fMocVIMUGsBlJq0Q45ruhETcJ5MkpcO1fIeBRBRr03hge2WYuZXyDzB8wrHRptiwzbVHEuEmxgCjKREPiPi69pcpvR3szEjU5k/rmxmuFsW6VbKtqcTtT1Qyr0tnd+3pd3YgU3vcxM+KzogWG7ZYU/AHLaDYIUnDQRd/ukR98PFXLa9Moq7L853KGYETrifcA4asnqtQLNu76r3JLZo+XikMbFXzTOwF3qTfbvWhN8+gMZgscdxN6MNFjdAJskNZcnjZnZeKYe7jiQj2rvnL7wZrB+DDU4rfb3ePsq4Yn4YBgWsdsy+LlEk+Wyt0xoGpisGUoX5Rj/PVjgFDd6Qbd9igYsU9KKHn3FarEA3DIMW/lNNb8pmydQy+/2UldfhgL/sAYi0EN+RFX8L9DFKD/4VftcHmAJWLOmf0pC4/mh7j7jxm6qle52cXdNOoIbN3K6i0PYjexLssNRoQ6YNxvmmz7SxJqbRiLW6jDNe1btC/j33ruTv025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rcIRtnyU1X0U9bf4L6cLUP2KpSbWMuqUcT6BPXW6oPpnXf+NsgHv5QK/dblJKtwQa1IFd6ELyUOLxrHuB2tDeyc8yWa00oSlmjloJqZZxyAq80ag0jI/eqyPM/aVJm4ZrOoWecB5Bo2dmnHmVbZ6yHsT8bZ2TAlnxMwj6UUJ0H8YLBvx2g2u3Nj6x2a926aCwhDb+3Mr0PntoahlIDtukveJn1z172qZOMb4OAoPu77+DdAV43cZfwn5dlw84RRTwK70Z10UUnC8veRBnsD5r16lchLqUm1q76XkCSmRpA14I91Bx2Hqmy74745L8UNGcT3RcMhadR5AEszi9HFlR01ZFRvKUmj+S7vpSTTuoObQZjOU+JqD2QSdt/7tp3vczgkbnDQz9et52NTzP9lQaSq5sSb6IOC6TxpN3MffnO7hurRhbrGW9g/JAsDB5MjRAbxYKSxzvqSL8yOaqpcLOcBZLr4fRcNIGwAjR+wqFcSSYKFMfwugDV2GHp4h7DoME6uVurPNYDzh6ooo7pcOHZcWsZZa5GS28XlgnSk48lAaZcn2aawRsZO0S7LNYXHuhQIR8F7M3RcNYcK9S/ikfSL+aOUf2YJZhfjCkLEQ+JCFrsnPQTKYxp9W9+fjwn8WVhd7cAAwpLofm7lW2FesBXDe5RaF8qaUonzIFtOGv6eaJTCyLFziN6Jv0WtUf1EcG12X4rSdiOUkw5Z6zZ7aqH6EkE9TJ/pASxJazGFaCo4CwKZNWmAJqXMSmHpOgXs2Vb+QBUb7JOaViM8NDHjSpux6DRqb83YrPXO5zxCOAhmEA3KA8jnhqgSwp5UBhBnX6IpM9KqbLBiSEf96uV0BthhIpfGSYB/T+kzW10F4ld5oQn6GmAyc8Bg3ReWor5V8zb/43oJqJutMtp2eyHU0K8UfofElSqNb9r0OTWlcbpp6vdCiiuoDu7RnYLUAX5yW0tEMf9xtIXrC/zBRl9pVon2cn+VZlhYsurFzWDHrNthWJhAz38+XDLtbBSXTR+JG9FKiJ7");

        for(tabldto table : listTableRespDto.getRows()){
            if(!table.getTableName().startsWith(req.getPrefix())){
                continue;
            }
            QueryTableInfoRespDto retData = client.queryTableInfo(table.getId(),"JSESSIONID=ceba142c-4d8d-494e-9d36-b9cbbc7742a7; rememberMe=pVUHSSx1v0U8dKN4EtPHZxWT7WdyeGFu6u0wI6asvnCi5HQjSUH7tvQlkC0fMocVIMUGsBlJq0Q45ruhETcJ5MkpcO1fIeBRBRr03hge2WYuZXyDzB8wrHRptiwzbVHEuEmxgCjKREPiPi69pcpvR3szEjU5k/rmxmuFsW6VbKtqcTtT1Qyr0tnd+3pd3YgU3vcxM+KzogWG7ZYU/AHLaDYIUnDQRd/ukR98PFXLa9Moq7L853KGYETrifcA4asnqtQLNu76r3JLZo+XikMbFXzTOwF3qTfbvWhN8+gMZgscdxN6MNFjdAJskNZcnjZnZeKYe7jiQj2rvnL7wZrB+DDU4rfb3ePsq4Yn4YBgWsdsy+LlEk+Wyt0xoGpisGUoX5Rj/PVjgFDd6Qbd9igYsU9KKHn3FarEA3DIMW/lNNb8pmydQy+/2UldfhgL/sAYi0EN+RFX8L9DFKD/4VftcHmAJWLOmf0pC4/mh7j7jxm6qle52cXdNOoIbN3K6i0PYjexLssNRoQ6YNxvmmz7SxJqbRiLW6jDNe1btC/j33ruTv025lY2VSjcti7HWIazhdCPieIm8FwDwYJmdu1bu3cZCJsObaI2s2GxlliAnx2qIGpKw059wqoRLvZVMG1Qgg6nz/sth56IYT11T1IaGOXZiNysbKT+yqdOFhT4BkpwhMsoqYeOqP/52ERtPJHf1N8FhHHJfvmwLj2uxxH/euVSIr0Xi8IKnvV2ib0IYD9+2WRXi7mRAih5yBdaQ1XOQNCuyzqD8KWTWRm492T5hIL0eR0u5AKxPvwWu8BEUfhAL8AVMC6qVht8U9rjh0+YQ5shw0RXKM1Q1Iq/CkbutaBz0/ICGzGmBgalBY8Cle4HwX/t/MMkcn3P58QZrs8ChTsadQ+prPB8NRdQTsa/W7yuPJEDGrp6BLMU2N+yaqI9H0vt5/e1AcSqmK8Qpg6We8fReI0RY+g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TxpN3MffnO7hurRhbrGW9g/JAsDB5MjRAbxYKSxzvqSL8yOaqpcLOcBZLr4fRcNIGwAjR+wqFcSSYKFMfwugDV2GHp4h7DoME6uVurPNYDzh6ooo7pcOHZcWsZZa5GS28XlgnSk48lAaZcn2aawRsZO0S7LNYXHuhQIR8F7M3RcNYcK9S/ikfSL+aOUf2YJZhfjCkLEQ+JCFrsnPQTKYxp9W9+fjwn8WVhd7cAAwpLofm7lW2FesBXDe5RaF8qaUonzIFtOGv6eaJTCyLFziN6Jv0WtUf1EcG12X4rSdiOUkw5Z6zZ7aqH6EkE9TJ/pASxJazGFaCo4CwKZNWmAJqXMSmHpOgXs2Vb+QBUb7JOaViM8NDHjSpux6DRqb83YrPXO5zxCOAhmEA3KA8jnhqgSwp5UBhBnX6IpM9KqbLBiSEf96uV0BthhIpfGSYB/T+kzW10F4ld5oQn6GmAyc8Bg3ReWor5V8zb/43oJqJutMtp2eyHU0K8UfofElSqNb9r0OTWlcbpp6vdCiiuoDu7RnYLUAX5yW0tEMf9xtIXrC/zBRl9pVon2cn+VZlhYsurFzWDHrNthWJhAz38+XDLtbBSXTR+JG9FKiJ7");
            if(retData==null || retData.getData()==null || retData.getData().size()==0){
                return OkrResult.success(retData);
            }
            for(TableInfo info : retData.getData()){
                if(info.getIsRequired()!=null && info.getIsRequired().equals("Y")){
                    String sql = "";
                    if(info.getMysqlFieldLength()==null){
                        sql = String.format("ALTER TABLE %s CHANGE COLUMN %s %s %s NOT NULL COMMENT '%s';",table.getTableEnName(), CommonFunctionHelper.humpToUnderline(info.getFieldEnName()),CommonFunctionHelper.humpToUnderline(info.getFieldEnName()),info.getMysqlFieldDataType(),info.getFieldName());
                    }else{
                        sql = String.format("ALTER TABLE %s CHANGE COLUMN %s %s %s NOT NULL COMMENT '%s';",table.getTableEnName(), CommonFunctionHelper.humpToUnderline(info.getFieldEnName()),CommonFunctionHelper.humpToUnderline(info.getFieldEnName()),info.getMysqlFieldDataType()+"(128)",info.getFieldName());
                    }
                    System.out.println(sql);
                }

            }
        }

        return OkrResult.success(null);
    }
}
